// facility/mapLoop/mapLoop.js
import http from '../../http'
Component({
	/**
	 * 组件的属性列表
	 */
	properties: {
		infoType: '',
		nowProjectInfo: '',
		nowfacilityInfo: '',
	},

	/**
	 * 组件的初始数据
	 */
	data: {
		loopSwitchBol: false,
		loopList: []
	},

	/**
	 * 组件的方法列表
	 */
	methods: {
		loopSwitch() {
			if (this.properties.infoType == 'project') {
				wx.showToast({
					title: '请选择回路',
					icon: 'error',
				})
			} else {
				this.setData({
					loopSwitchBol: true
				})
				http.get({
					url: '/zg/device-instance/get',
					data: {
						id: this.properties.nowfacilityInfo.id
					},
					then: (res) => {
						console.log('回路信息', res);
						let list = JSON.parse(res.data.data.deviceMetadata)
						list = list.map((item, index) => {
							return {
								name: '回路 ' + (index + 1),
								control: item == 1 ? false : true
							}
						})
						this.setData({
							loopList: list
						})
					}
				})
			}
		},
		loopSwitchClose() {
			this.setData({
				loopSwitchBol: false
			})
		},
		loopSwitchConfirm() {
			let switchList = this.data.loopList.map(item => {
				return item.control ? 0 : 1
			})
			http.post({
				url: '/zg/command/one',
				data: {
					command: "LAMP_CONTROL",
					brightness: 0,
					deviceId: this.properties.nowfacilityInfo.id,
					control: switchList
				},
				then: (res) => {
					console.log('回路开关', res);
					if (res.data.code == 0) {
						wx.showToast({
							title: '下发成功',
							icon: 'success',
						})
					}
				}
			})
		},
		onChange(e) {
			console.log(e.currentTarget.dataset.index, e.detail);
			let list = this.data.loopList
			list[e.currentTarget.dataset.index].control = e.detail
			this.setData({
				loopList: list
			})
		}
	}
})